<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../../res/layui/css/layui.css"/>
    <script type="text/javascript" src="../../res/js/jquery-3.4.1.min.js"></script>
    <script type="text/javascript" src="../../res/layui/layui.js"></script>
    <script type="text/javascript" src="../../res/module/treeSelect/treeSelect.js"></script>
    <script type="text/javascript" src="../../res/js/tools.js"></script>
    <script type="text/javascript">
        //全局同步
        $.ajaxSetup({
            async: false
        });
    </script>
    <style type="text/css">
        .layui-input-block {
            width: 70%;
        }

        .layui-form-label {
            width: 80px;
        }

        body {
            padding-top: 10px;
        }
    </style>
</head>
<body>
<form class="layui-form" id="modelForm">


    <!--上级模块-->
    <div class="layui-form-item">
        <label class="layui-form-label">上级模块</label>
        <div class="layui-input-block">
            <input type="text" name="moduleIds" id="tree" lay-filter="tree" class="layui-input">
        </div>
    </div>


    <!--模块名称-->
    <div class="layui-form-item">
        <label class="layui-form-label">模块名称</label>
        <div class="layui-input-block">
            <input type="text" name="moduleName" id="moduleName" lay-verify="title" autocomplete="off"
                   placeholder="模块名称"
                   class="layui-input">
        </div>
    </div>


    <!--URL地址-->
    <div class="layui-form-item">
        <label class="layui-form-label">URL地址</label>
        <div class="layui-input-block">
            <!--主键-->
            <input type="hidden" name="moduleId" id="moduleId" lay-verify="title">
            <input type="text" name="moduleUrl" id="moduleUrl" lay-verify="title" autocomplete="off"
                   placeholder="网址,若要设置为一级模块可以不填"
                   class="layui-input">
        </div>
    </div>


    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>


</form>

<script type="text/javascript">
    // 加载formSelect模块
    layui.config({
        base: "../../res/module/"
    }).extend({
        treeSelect: "treeSelect/treeSelect"
    }).use(['treeSelect', 'form', 'layer'], function () {
        var treeSelect = layui.treeSelect,
            form = layui.form,
            $ = layui.jquery,
            layer = layui.layer;

        treeSelect.render({
            // 选择器
            elem: '#tree',
            // 数据
            data: '../../model/findAllModel.form',
            // 异步加载方式：get/post，默认get
            type: 'get',
            // 占位符
            placeholder: '不选则为根模块',
            // 是否开启搜索功能：true/false，默认false
            search: true,
            style: {
                folder: {
                    enable: false
                },
                line: {
                    enable: true
                }
            },
            // 点击回调
            click: function (d) {
                console.log(d);
            },
            // 加载完成后的回调函数
            success: function (d) {
                console.log(d);

                var id = getQueryString("id");
                console.info(id);
                if (id != null && id != "" && id != "undefine") {
                    var url = "../../model/findById.form";
                    var data = {id: id};
                    $.post(url, data, function (obj) {
                        console.info(obj.moduleId + "====" + obj.moduleUrl + "=====" + obj.moduleName);
                        if(obj.moduleIds) {
                            treeSelect.checkNode('tree', obj.moduleIds);//上级模块
                        }
                        $("#moduleId").val(obj.moduleId);//主键
                        $("#moduleUrl").val(obj.moduleUrl);//url地址
                        $("#moduleName").val(obj.moduleName);//模块名称
                    }, "json");
                }

            }
        })
    });


    layui.use(['form', 'layedit', 'laydate'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;

        // //创建一个编辑器
        // var editIndex = layedit.build('LAY_demo_editor');
        //
        // //自定义验证规则
        // form.verify({
        //     title: function (value) {
        //         if (value.length < 1) {
        //             return '标题至少得1个字符啊';
        //         }
        //     }
        //     , content: function (value) {
        //         layedit.sync(editIndex);
        //     }
        // });
        //监听提交
        form.on('submit(formDemo)', function (data) {
            var url = "../../model/save.form";

            var datas=JSON.stringify(data.field)
            datas=JSON.parse(datas);
            // var formData=$('form').serializeArray();
            console.info("---");
            console.info(datas);
            $.post(url,datas,function (objs) {
                console.info("运算完成")
                if(objs.state==1){
                    console.info("增加成功")
                    window.parent.parent.location.reload();//刷新父页面
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }
            },"json");

        });
    });
</script>
</body>
</html>